package Constructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import FrameWork.Direction;
import Main.SpaceGame;
import MiscelaneousEntities.Explosion;
import Projectiles.Projectile;
import World.TileType;

public class PPS extends Construction
{
	int cooldown = 0;
	boolean canFire;
	Direction orientation;
	BufferedImage spriteReady = null, spriteFired = null;
	private Projectile target;
	boolean drawingLine = false;
	int drawCnt = 10;
	int drawX,drawY;
	public int pixelRadius = 200;

	public PPS(int x, int y, SpaceGame main) 
	{
		super(x, y, main);
		drawX = 0;drawY = 0;
		this.x = x;
		this.y = y;
		this.main = main;
		height = 1; width = 1;
		workingTile = main.world.tiles[x][y];
		canFire = true;
		checkOrientation();
		target = null;
		try {
			spriteReady = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"PPSUnfired.png"));
			spriteFired = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"PPSFired.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void draw(Graphics g) 
	{
		BufferedImage sprite = null;
		if(canFire)
			sprite = spriteReady;
		else
			sprite = spriteFired;
		if(drawingLine == true)
		{
			g.setColor(Color.GREEN);
			g.drawLine(x*10, y*10, drawX, drawY);
			g.drawLine(x*10-1, y*10, drawX-1, drawY);
			g.drawLine(x*10+1, y*10, drawX+1, drawY);
			g.drawLine(x*10, y*10-1, drawX, drawY-1);
			g.drawLine(x*10, y*10+1, drawX, drawY+1);
			drawCnt--;
			if(drawCnt <= 0)
				drawingLine = false;
		}
		
		double rotationRequired = Math.toRadians(orientation.getRotationRequired());
		double locationX = sprite.getWidth() / 2;
		double locationY = sprite.getHeight() / 2;
		AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
		
		g.drawImage(op.filter(sprite, null), x*10, y*10, null);
		drawFlash(g, x, y);
	}
	
	public void update()
	{
		if(cooldown > 0)
			cooldown--;
		if(cooldown > 0)
			canFire = false;
		else
			canFire = true;
		
		target = null;
		if(canFire)
		{
			for(int i = 0;i<main.projectileManager.projectileList.size();i++)
			{
				Projectile current = main.projectileManager.projectileList.get(i);
				if(target == null)
				{
					if(Math.pow(current.x - x*10-.5,2) + Math.pow(current.y - y*10-.5, 2) < pixelRadius*pixelRadius && !current.friendly)
						target = current;
				}
				else if(Math.pow(current.x - x*10-.5,2) + Math.pow(current.y - y*10-.5, 2) < Math.pow(target.x - x*10-.5,2) + Math.pow(target.y - y*10-.5, 2) && !current.friendly)
					target = current;
			}
		}
		if(canFire && target != null && !target.friendly)
		{
			drawCnt = 10;
			cooldown = 100;
			drawX = (int)(target.x+.5)+5;
			drawY = (int)(target.y +.5)+5;
			drawingLine = true;
			main.miscManager.miscList.add(new Explosion((int)(target.x/10+.5), (int)(target.y/10+.5), target.blastRadius, main));
			main.projectileManager.projectileList.remove(target);
			target = null;
			canFire = false;
		}
	}

	@Override
	public void setText() 
	{
		textColorIndex = 2;
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("Projectile");
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("Protection System");
	}

	@Override
	public String getName() 
	{
		return "PPS";
	}
	
	@Override
	public void keyTyped(KeyEvent e) 
	{
		
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		
	}
	
	public void checkOrientation()
	{
		try
		{
			if(main.world.tiles[x-1][y].getType() == TileType.Interior && main.world.tiles[x+1][y].getType() == TileType.Space)
				orientation = Direction.right;
			else if(main.world.tiles[x+1][y].getType() == TileType.Interior && main.world.tiles[x-1][y].getType() == TileType.Space)
				orientation = Direction.left;
			else if(main.world.tiles[x][y+1].getType() == TileType.Interior && main.world.tiles[x][y-1].getType() == TileType.Space)
				orientation = Direction.up;
			else if(main.world.tiles[x][y-1].getType() == TileType.Interior && main.world.tiles[x][y+1].getType() == TileType.Space)
				orientation = Direction.down;
			else
				orientation = Direction.right;
		}
		catch(ArrayIndexOutOfBoundsException e)
		{}
	}
}
